package com.zwh.modbus.modbus4j;

import java.util.ArrayList;
import java.util.List;

import com.zwh.modbus.modbus4j.param.TConnectDevice;
import com.zwh.modbus.modbus4j.param.TConnectDevicePoint;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author ody
 * @Description //TODO
 * @Date 2023/7/21 17:32
 **/
public abstract class AbstractDeviceProtocolService implements DeviceProtocolService {

    private TConnectDevice connectDevice;

    private RemoteConnectDeviceService remoteConnectDeviceService;

    private ThreadPoolTaskExecutor storageDataThreadPool;

    protected Boolean isCancel = false;

    private Integer offlineCount = 0;

    public AbstractDeviceProtocolService(TConnectDevice connectDevice,RemoteConnectDeviceService remoteConnectDeviceService,
                                         ThreadPoolTaskExecutor storageDataThreadPool){
        this.connectDevice = connectDevice;
        this.remoteConnectDeviceService = remoteConnectDeviceService;
        this.storageDataThreadPool = storageDataThreadPool;
    }

    @Override
    public TConnectDevice getDeviceInfo() {
        return this.connectDevice;
    }

    @Override
    public List<TConnectDevicePoint> getDevicePointsInfo(String attributeName){
        List<TConnectDevicePoint> list = null;
        if(StringUtils.isEmpty(attributeName)){
            list =  connectDevice.getConnectDevicePoints();
        }else {
            List<TConnectDevicePoint> points = connectDevice.getConnectDevicePoints();
            for(TConnectDevicePoint point : points) {
                if(attributeName.equals(point.getPointIden())){
                    list = new ArrayList<>();
                    list.add(point);
                    break;
                }
            }
        }
        return list;
    }

    /**
      * @Author ody
      * @Description //TODO 更新设备在线离线状态
      * @Date 2023/7/21 17:56
      * @Param [data]
      * @return void
     **/
    protected void updateDeviceStatus(JSONObject data) {
//        if (data.size() == 0 && connectDevice.getStatus() == 1) {
//            try {
//                //离线
//                if(offlineCount >= 3){//超过三次，判定离线
//                    remoteConnectDeviceService.updateStatus(connectDevice.getId(),0);
//                    connectDevice.setStatus(0);
//                }else{
//                    offlineCount++;
//                }
//            }catch (Exception e){}
//        }else if(data.size() > 0 && connectDevice.getStatus() == 0){
//            try {
//                //在线
//                remoteConnectDeviceService.updateStatus(connectDevice.getId(),1);
//                connectDevice.setStatus(1);
//                offlineCount = 0;
//            }catch (Exception e){}
//        }else if(data.size() > 0){
//            offlineCount = 0;
//        }
    }

    protected void storageCollectData(JSONObject data){
        //采集数据实时缓存起来
//        StorageCollectDataTask dataTask = new StorageCollectDataTask(connectDevice,data);
//        storageDataThreadPool.execute(dataTask);
    }

    @Override
    public boolean isConnect(String deviceId) {
        if(connectDevice.getStatus() == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void cancel() {
        this.isCancel = true;
    }
}
